<?php
/*
	Copyright © Eleanor CMS
	URL: http://eleanor-cms.ru, http://eleanor-cms.su, http://eleanor-cms.com, http://eleanor-cms.net, http://eleanor.su
	E-mail: support@eleanor-cms.ru, support@eleanor.su
	Developing: Alexander Sunvas*
	Interface: Rumin Sergey
	=====
	*Pseudonym
*/
if(!defined('CMS'))die;
global$Eleanor;

$conf=include($Eleanor->module['path'].'config.php');
$Eleanor->mconfig = include($Eleanor->module['path'] . 'config.php');
Eleanor::$Language->Load($Eleanor->module['path'] . 'lang_user-*.php', $Eleanor->mconfig['n']);
if(Eleanor::$Cache->Get($conf['n'].'-runned')===false)
{
	Eleanor::$Cache->Put($conf['n'].'-runned',true,100);
	###################
	$r=$R = Eleanor::$Db->Query('SELECT `id`,`email`,`title`,`pic`,`text` FROM `'.$conf['t'].'` INNER JOIN `'.$conf['tl'].'` USING (`id`) WHERE `end_day`=1 AND `status`=1 AND `date_end`<=\''.date('Y-m-d').'\'');
	if (Eleanor::$Db->NumRows($r)>0)
	{
		$lang = Eleanor::$Language[$GLOBALS['Eleanor']->mconfig['n']];
		$Email=new Email;
	
		while($arr=$R->fetch_assoc($r))
		{
			Eleanor::$Db->Delete($conf['t'], '`id`=' . $arr['id'] . ' LIMIT 1');
			Eleanor::$Db->Delete($conf['tl'], '`id`=' . $arr['id']);
			Eleanor::$Cache->Lib->CleanByTag($Eleanor->mconfig['n']);
			
			$pi=explode('||',$arr['pic']);
			foreach($pi as $p)
			{	
				if(is_file($p))
				{
					unlink($p);
				}
				elseif(is_file(str_replace('_preview','',$p)))
				{
					unlink(str_replace('_preview','',$p));					
				}
			}
			
			$Email->parts=array(
			'multipart'=>'mixed',
			array(
			'content-type'=>'text/html',
			'charset'=>DISPLAY_CHARSET,
			'content'=>$lang['end'].'<br/><br/>'.$arr['text'],
			),
			);

			#$lang=Eleanor::FilterLangValues($langs,$v['language']);
			$Email->subject='Auto '.$arr['title'];
			$Email->from='auto@auto.lv';
			#$Email->notice=0;
			$Email->notice_on=false;
			$Email->Send(array('to'=>$arr['email']));
			#echo'<pre>';
			#print_r($Email);
		}
	}
	
	$time =strtotime("+2 day");
	$dateend=date("Y-m-d H:i:s",$time);
	
	$r=$R = Eleanor::$Db->Query('SELECT `id`,`email`,`title`,`text` FROM `'.$conf['t'].'` INNER JOIN `'.$conf['tl'].'` USING (`id`) WHERE `end_day`=0 AND `status`=1 AND `date_end`<=\''.$dateend.'\'');
	
	#echo'<pre>';
	#print_r($dateend);
	#echo$dateend;
	#die();
	if (Eleanor::$Db->NumRows($r)>0)
	{
		$lang = Eleanor::$Language[$GLOBALS['Eleanor']->mconfig['n']];
		$Email=new Email;
		
		while($arr=$R->fetch_assoc($r))
		{
			
			$Email->parts=array(
			'multipart'=>'mixed',
			array(
			'content-type'=>'text/html',
			'charset'=>DISPLAY_CHARSET,
			'content'=>$lang['end2'].'<br/><br/>'.$arr['text'],
			),
			);

			$Email->subject='Auto '.$arr['title'];
			$Email->from='auto@auto.lv';
			#$Email->notice=0;
			$Email->notice_on=false;
			#echo'<pre>';
	#print_r($Email);
	#echo$dateend;
	#die();
			if($Email->Send(array('to'=>$arr['email'])))
				Eleanor::$Db->Update($Eleanor->mconfig['tl'], array('end_day' => 1), '`id`=' . $arr['id'] . ' LIMIT 1');

		}
	}
	#echo'a';
	#die();
	######################################################

	$n=150;

	if($n>0)
	{
		$r=$R = Eleanor::$Db->Query('SELECT `id`,`pinned` FROM `'.$conf['t'].'` WHERE `status`=1 AND `date`<=\''.date('Y-m-d H:i:s').'\' AND `pinned` IS NOT NULL LIMIT '.$n);
		while($arr=$R->fetch_assoc($r))
		Eleanor::$Db->Update($conf['t'],array('date'=>$arr['pinned'],'pinned'=>null),'`id`='.$arr['id'].' LIMIT 1');

		$R = Eleanor::$Db->Query('SELECT UNIX_TIMESTAMP(`date`) FROM `'.$conf['t'].'` WHERE `status`=1 AND `date`>\''.date('Y-m-d H:i:s').'\' AND `pinned` IS NOT NULL LIMIT 1');
		list($near)=Eleanor::$Db->FetchRow();
		$near=(int)$near;
		$totomor=strtotime('+1 DAY',mktime(0,0,0));
		Eleanor::$Cache->Put($conf['n'].'_nextrun',$near>0 ? min($near,$totomor) : $totomor);
	}
	Eleanor::$Cache->Delete($conf['n'].'-runned');
}
Start();
?>